package com.cdu.guoxinan.www.smartcom.controller.propertyController;

import com.cdu.guoxinan.www.smartcom.aop.Annotation.OperationLog;
import com.cdu.guoxinan.www.smartcom.domian.dto.AddRequestDTO;
import com.cdu.guoxinan.www.smartcom.domian.dto.RepairApplyDTO;
import com.cdu.guoxinan.www.smartcom.domian.dto.RequestDTO;
import com.cdu.guoxinan.www.smartcom.domian.vo.RepairApplyVO;
import com.cdu.guoxinan.www.smartcom.domian.vo.SelectOption;
import com.cdu.guoxinan.www.smartcom.service.RepairApplyService;
import com.cdu.guoxinan.www.smartcom.unit.model.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/apply")
@Api(tags = "报修申请")
public class RepairApplyController {

    @Resource
    private RepairApplyService repairApplyService;

    @GetMapping("/all")
    @ApiModelProperty("获取所有报修申请")
    @OperationLog(description = "获取所有报修申请")
    public R getAllRepairs(  RepairApplyDTO dto) {
        System.out.println("dto:"+dto);
        PageHelper.startPage(dto.getPage(), dto.getLimit());

        PageInfo<RepairApplyVO> pageInfo = repairApplyService.getAllRepairs(dto);
        return R.ok(pageInfo);
    }

    @GetMapping("/priorities")
    @ApiModelProperty("获取优先级选项")
    @OperationLog(description = "获取优先级选项")
    public R getPriorityOptions() {
        List<SelectOption> options = repairApplyService.getPriorityOptions();
        return R.ok(options);
    }

    @GetMapping("/status")
    @ApiModelProperty("获取报修状态选项")
    @OperationLog(description = "获取报修状态选项")
    public R getStatusOptions() {
        List<SelectOption> options = repairApplyService.getStatusOptions();
        return R.ok(options);
    }

    @PostMapping("/add")
    @ApiModelProperty("新增报修申请")
    @OperationLog(description = "新增报修申请")
    public R addRepairApply(@RequestBody AddRequestDTO dto) {
        int rows = repairApplyService.addRepairApply(dto);
        return rows > 0 ? R.ok("添加成功") : R.error("添加失败");
    }

    @PostMapping("/update")
    @ApiModelProperty("更新报修申请")
    @OperationLog(description = "更新报修申请")
    public R updateRepairApply(@RequestBody RequestDTO requestDTO) {
        int rows = repairApplyService.updateRepairApply(requestDTO);
        return rows > 0 ? R.ok("修改成功") : R.error("修改失败");
    }

    @PostMapping("/delete")
    @ApiModelProperty("删除单个报修申请")
    @OperationLog(description = "删除单个报修申请")
    public R deleteRepairApply(@RequestParam Long requestId) {
        int rows = repairApplyService.deleteRepairApply(requestId);
        return rows > 0 ? R.ok("删除成功") : R.error("删除失败");
    }

    @PostMapping("/deleteBatch")
    @ApiModelProperty("批量删除报修申请")
    @OperationLog(description = "批量删除报修申请")
    public R deleteBatch(@RequestBody Integer[] ids) {
        int rows = repairApplyService.deleteBatch(ids);
        return rows == ids.length? R.ok("全部删除成功") : R.error("部分或全部删除失败");
    }
}
